{extend name="common/layout" /}

{block name="body"}
<div class="container">
    <!-- 搜索栏 -->
    <form class="layui-form layui-search">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label for="name" class="layui-form-label">分组名称：</label>
                <div class="layui-input-inline">
                    <input type="text" id="name" name="name" class="layui-input" placeholder="请输入分组名称" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-sm layui-btn-default" lay-submit lay-filter="search">查询</a>
                <a class="layui-btn layui-btn-sm layui-btn-primary" lay-submit lay-filter="clear-search">重置</a>
            </div>
        </div>
    </form>

    <!-- 表格栏 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="wait-table-list" lay-filter="wait-table-list"></table>
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <a class="layui-btn layui-btn-sm layui-btn-default {:check_perms('add')}" lay-event="add">
                        <i class="layui-icon icon-add"></i>
                        <span>新增</span>
                    </a>
                </div>
            </script>
            <script type="text/html" id="table-operate">
                <button type="button" class="layui-btn layui-btn-xs layui-btn-default {:check_perms('edit')}" lay-event="edit">
                    <i class="layui-icon icon-edit"></i>
                </button>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-danger {:check_perms('del')}" lay-event="del">
                    <i class="layui-icon icon-del"></i>
                </button>
            </script>
        </div>
    </div>
</div>
{/block}

{block name="js"}
<script>
    layui.use(function() {
        // 渲染表格
        let table = waitUtil.table({
            elem: '#wait-table-list'
            ,url: '{:route("user.group/index")}'
            ,cols: [[
                {type:'checkbox', width:48},
                {field:'id', title:'ID', width:70, align:'center', event:'id'},
                {field:'name', title:'分组名称', minWidth:150, align:'center'},
                {field:'remarks', title:'备注', minWidth:150},
                {field:'sort', title:'排序', minWidth:80, align:'center'},
                {field:'create_time', title:'创建时间', minWidth:170, align:'center'},
                {fixed:'right', title:'操作', width:90, align:'center', toolbar:'#table-operate'}
            ]]
        });

        // 逻辑事件
        waitUtil.event({
            add: function () {
                waitUtil.popup({
                    title: '新增',
                    url: '{:route("user.group/add")}',
                    area: ['400px', '350px'],
                    success: function (layero, index) {
                        layero.layui.form.on('submit(addForm)', function(data){
                            waitUtil.ajax({
                                url: '{:route("user.group/add")}',
                                type: 'POST',
                                data: data.field
                            }).then((res) => {
                                if (res.code === 0) {
                                    table.reload({page: {curr: 1}});
                                    layer.close(index);
                                }
                            });
                        });
                    }
                });
            },
            edit: function (obj) {
                waitUtil.popup({
                    title: '编辑',
                    url: '{:route("user.group/edit")}?id='+obj.data.id,
                    area: ['400px', '350px'],
                    success: function (layero, index) {
                        layero.layui.form.on('submit(addForm)', function(data){
                            data.field['id'] = obj.data.id;
                            waitUtil.ajax({
                                url: '{:route("user.group/edit")}',
                                type: 'POST',
                                data: data.field
                            }).then((res) => {
                                if (res.code === 0) {
                                    table.reload();
                                    layer.close(index);
                                }
                            });
                        });
                    }
                });
            },
            del: function (obj) {
                layer.confirm('确定要删除此项数据吗？', function(index) {
                    layer.close(index);
                    waitUtil.ajax({
                        url: '{:route("user.group/del")}',
                        type: 'POST',
                        data: {id: obj.data.id}
                    }).then((res) => {
                        if (res.code === 0) {
                            table.reload();
                        }
                    });
                });
            }
        });

        // 搜索事件
        waitUtil.search(table);
    });
</script>
{/block}